{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "gold_data=pd.read_csv('LBMA-GOLD.csv')\n",
    "bit_data=pd.read_csv('BCHAIN-MKPRU.csv')\n",
    "a_g=np.array(np.array(range(1,len(gold_data['Date'])+1)))\n",
    "b_g=np.array(gold_data['Date'])\n",
    "a_b=np.array(np.array(range(1,len(bit_data['Date'])+1)))\n",
    "b_b=np.array(bit_data['Date'])\n",
    "table_g=np.vstack([a_g,b_g]).T\n",
    "table_b=np.vstack([a_b,b_b]).T\n",
    "\n",
    "def dayb2dayg(day_b):\n",
    "    try:\n",
    "        for li in table_b:\n",
    "            if li[0]==day_b:\n",
    "                date=li[1]\n",
    "        for li in table_g:\n",
    "            if li[1]==date:\n",
    "                day_g=li[0]\n",
    "        return day_g,date\n",
    "    except:\n",
    "        return (False,date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "bit_data=np.array(bit_data)\n",
    "gold_data=np.array(gold_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "pre_g=pd.read_csv('prediction_g.csv',header=None)\n",
    "pre_b=pd.read_csv('prediction_b.csv',header=None)\n",
    "pre_g=np.array(pre_g)\n",
    "pre_g[:,0].astype(int)\n",
    "pre_b=np.array(pre_b)\n",
    "pre_b[:,0].astype(int)\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "609.96"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pre_b[5-5][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "设天数为day_b取该日两产品价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# today is day_b (day_b>=5)\n",
    "def day_info(day_b):\n",
    "    if day_b<5:\n",
    "        raise ValueError('day_b must bigger than 5')\n",
    "    if dayb2dayg(day_b)[0]!=False:\n",
    "        (day_g,date)=dayb2dayg(day_b)\n",
    "        value_b=bit_data[day_b-1][1]\n",
    "        value_b_p=pre_b[day_b-5][1]\n",
    "        value_b_pp=pre_b[day_b-5][2]\n",
    "        value_g=gold_data[day_g-1][1]\n",
    "        value_g_p=pre_g[day_g-5][1]\n",
    "        value_g_pp=pre_g[day_g-5][2]\n",
    "        info=(True,value_g,value_g_p,value_g_pp,value_b,value_b_p,value_b_pp,date)\n",
    "        return info\n",
    "    else: # gold cannot be treated this day\n",
    "        date=dayb2dayg(day_b)[1]\n",
    "        value_b=bit_data[day_b-1][1]\n",
    "        value_b_p=pre_b[day_b-5][1]\n",
    "        value_b_pp=pre_b[day_b-5][2]\n",
    "        info=(False,value_b,value_b_p,value_b_pp,date)\n",
    "        return info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, 1794.6, 1817.6, 1836.1, 46368.69, 47281.0, 48644.0, '9/10/21')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day_info(1826)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'n_g_i' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16848/2350858763.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     29\u001b[0m             \u001b[0mn_b_i\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mn_b\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mn_c\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mR2\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb_increase1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mg_increase1\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mb_increase1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0malpha_b\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0minfo\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     30\u001b[0m             \u001b[0mn_c_i\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mn_c\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mR2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 31\u001b[1;33m         \u001b[0mn_g\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mn_g_i\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     32\u001b[0m         \u001b[0mn_b\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mn_b_i\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     33\u001b[0m         \u001b[0mn_c\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mn_c_i\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'n_g_i' is not defined"
     ]
    }
   ],
   "source": [
    "alpha_g=0.01\n",
    "alpha_b=0.02\n",
    "n_c=1000\n",
    "n_g=0\n",
    "n_b=0\n",
    "R=10\n",
    "R2=0.1\n",
    "for day_b in range(5,1827):\n",
    "    info=day_info(day_b)\n",
    "    if info[0]==True: # gold can be treated\n",
    "        g_increase1=(info[2]-info[1])/info[1]\n",
    "        g_increase2=(info[3]-info[1])/info[1]\n",
    "        b_increase1=(info[5]-info[4])/info[4]\n",
    "        b_increase2=(info[6]-info[4])/info[4]\n",
    "        if g_increase1<-0.01 and b_increase1<-0.02:\n",
    "            n_g_i=n_g*(1+g_increase1*R)\n",
    "            n_b_i=n_b*(1+b_increase1*R)\n",
    "            n_c_i=n_c+n_g*abs(g_increase1)*R*(1-alpha_g)+n_b*abs(b_increase1)*R*(1-alpha_b)\n",
    "        if g_increase1<-0.01 and b_increase1>0.02:\n",
    "            n_g_i=n_g*(1+g_increase1*R)\n",
    "            n_b_i=n_b+(n_g*abs(g_increase1)*R)*info[1]*(1-alpha_g)/(info[4]*(1+alpha_b))\n",
    "            n_c_i=n_c\n",
    "        if g_increase1>0.01 and b_increase1<-0.02:\n",
    "            n_b_i=n_b*(1+b_increase1*R)\n",
    "            n_g_i=n_g+(n_b*abs(b_increase1)*R)*info[4]*(1-alpha_g)/(info[1]*(1-alpha_b))\n",
    "            n_c_i=n_c\n",
    "        if g_increase1>0.01 and b_increase1>0.02:\n",
    "            n_g_i=n_g+n_c*R2*(g_increase1/(g_increase1+b_increase1))*(1-alpha_g)/info[1]\n",
    "            n_b_i=n_b+n_c*R2*(b_increase1/(g_increase1+b_increase1))*(1-alpha_b)/info[4]\n",
    "            n_c_i=n_c*(1-R2)\n",
    "        n_g=n_g_i\n",
    "        n_b=n_b_i\n",
    "        n_c=n_c_i\n",
    "        # print(g_increase1,b_increase1)\n",
    "    elif info[0]==False:\n",
    "        b_increase1=(info[2]-info[1])/info[1]\n",
    "        b_increase2=(info[3]-info[1])/info[1]\n",
    "        if b_increase1>0.02:\n",
    "            n_b_i=n_b+n_c*R2/info[1]\n",
    "            n_c_i=n_c-n_c*R2-n_c*R2*alpha_b\n",
    "            n_g_i=n_g\n",
    "        if b_increase1<-0.02:\n",
    "            n_b_i=n_b*(1+b_increase1*R)\n",
    "            n_c_i=n_c+n_b*b_increase1*R*info[1]*(1-alpha_b)\n",
    "            n_g_i=n_g\n",
    "        n_g=n_g_i\n",
    "        n_b=n_b_i\n",
    "        n_c=n_c_i\n",
    "    print(n_c_i,n_g_i,n_b_i)\n",
    "print(n_c_i+n_g_i*1794.6+n_b_i*46368.69)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000.0"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info=day_info(1826)\n",
    "sum=n_c+n_g*info[1]+n_b*info[4]\n",
    "sum\n",
    "print(n_c_i+n_g_i*1794.6+n_b_i*46368.69)\n",
    "print(n_c_i,n_g_i,n_b_i)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b5fc3e43e814ad01a3162358639356c8d5269fa9e7a0a06af2b9cdf0ba130cad"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
